package cn.edu.pet.repo;


import cn.edu.pet.db.BaseRepo;
import cn.edu.pet.db.ResultSetVisitor;
import cn.edu.pet.entity.AdoptRecord;
import cn.edu.pet.entity.Pets;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdoptRecordRepo extends BaseRepo {
    private AdoptRecordRepo() {

    }

    private static AdoptRecordRepo instance = new AdoptRecordRepo();

    public static AdoptRecordRepo getInstance() {
        return instance;
    }


    public boolean insertAdoptRecord(AdoptRecord record) {
        String sql = String.format("insert into adopt_record(user_id,pet_id,status,created_time) value('%s','%s','%s','%s')", record.getUserId(), record.getPetId(), record.getStatus(), new Timestamp(new Date().getTime()));
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<AdoptRecord> selectList(Integer status) {
        String sql;
        if (status != null) {
            sql = String.format("select ar.*,u.username,p.name from adopt_record ar inner join user u on ar.user_id = u.id inner join pets p on ar.pet_id = p.id where ar.status = %s", status);
        } else {
            sql = "select ar.*,u.username,p.name from adopt_record ar inner join user u on ar.user_id = u.id inner join pets p on ar.pet_id = p.id";
        }
        List<AdoptRecord> list = new ArrayList<>();
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet resultSet) throws SQLException {
                AdoptRecord adoptRecord = new AdoptRecord();
                adoptRecord.setId(resultSet.getInt(1));
                adoptRecord.setUserId(resultSet.getInt(2));
                adoptRecord.setPetId(resultSet.getInt(3));
                adoptRecord.setStatus(resultSet.getInt(4));
                adoptRecord.setCreatedTime(resultSet.getDate(5));
                adoptRecord.setUsername(resultSet.getString(6));
                adoptRecord.setPetName(resultSet.getString(7));
                list.add(adoptRecord);
            }
        });
        return list;
    }


    public List<AdoptRecord> selectListWithUser(Integer userId) {
        String sql = String.format("select ar.*,u.username,p.name,p.type from adopt_record ar inner join user u on ar.user_id = u.id inner join pets p on ar.pet_id = p.id where ar.user_id = %s", userId);
        List<AdoptRecord> list = new ArrayList<>();
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet resultSet) throws SQLException {
                AdoptRecord adoptRecord = new AdoptRecord();
                adoptRecord.setId(resultSet.getInt(1));
                adoptRecord.setUserId(resultSet.getInt(2));
                adoptRecord.setPetId(resultSet.getInt(3));
                adoptRecord.setStatus(resultSet.getInt(4));
                adoptRecord.setCreatedTime(resultSet.getDate(5));
                adoptRecord.setUsername(resultSet.getString(6));
                adoptRecord.setPetName(resultSet.getString(7));
                adoptRecord.setPetType(resultSet.getString(8));
                list.add(adoptRecord);
            }
        });
        return list;
    }

    public boolean deleteAdoptRecord(Integer id) {
        String sql = String.format("delete from adopt_record where id = %s", id);
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public boolean updateAdoptRecord(AdoptRecord adoptRecord) {
        String sql = String.format("update adopt_record set status = '%s' where id = %s", adoptRecord.getStatus(), adoptRecord.getId());
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
